package com.xyzwps.webfluxjava.handler;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import lombok.Builder;
import lombok.Data;
import lombok.val;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.net.URI;

import static org.springframework.web.reactive.function.server.ServerResponse.*;

@Component
public class PageCommonHandlers {

    public Mono<ServerResponse> index(ServerRequest req) {
        return permanentRedirect(URI.create("/home")).build();
    }

    public Mono<ServerResponse> home(ServerRequest req) {
    	val url = "http://p5.so.qhimgs1.com/bdr/_240_/t01ea45ae910178aedb.jpg";
        return ok().render("home", ImmutableMap.of(
        		"blocks", ImmutableList.of(
                		HomePageBlockDto.builder().title("博客").subTitle("都是胡乱写的东西").url(url).build(),
                		HomePageBlockDto.builder().title("教程").subTitle("也是胡乱写的东西").url(url).build(),
                		HomePageBlockDto.builder().title("两只手").subTitle("这是还没写的东西").url(url).build()
        		)
		));
    }

}

@Builder
@Data
class HomePageBlockDto {
	private String title;
	private String subTitle;
	private String url;
}
